/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormularioUsuario.java
 *
 * Created on 8/01/2011, 04:39:18 PM
 */
package ventanas.pasosiniciales;

import beans.Estado;
import beans.TipoUsuario;
import beans.Usuario;
import db.Model;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Usuario
 */
public class FormularioUsuario extends javax.swing.JPanel {

    /** Creates new form FormularioUsuario */
    public FormularioUsuario() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tpfpassword = new javax.swing.JPasswordField();
        tpfrpassword = new javax.swing.JPasswordField();
        txtusuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(86, 177, 185));

        tpfpassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tpfpasswordKeyTyped(evt);
            }
        });

        tpfrpassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tpfrpasswordKeyTyped(evt);
            }
        });

        txtusuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtusuarioKeyTyped(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Usuario");

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Password");

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Repita Password");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tpfrpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tpfpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tpfpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tpfrpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(107, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtusuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtusuarioKeyTyped
        // TODO add your handling code here:
               if ( txtusuario.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtusuarioKeyTyped

    private void tpfpasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tpfpasswordKeyTyped
        // TODO add your handling code here:
        if ( tpfpassword.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_tpfpasswordKeyTyped

    private void tpfrpasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tpfrpasswordKeyTyped
        // TODO add your handling code here:
                if ( tpfrpassword.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_tpfrpasswordKeyTyped

    public Usuario getUsuario() {
        Usuario us = new Usuario();
        us.setUsuario(txtusuario.getText());
        us.setPassword(tpfpassword.getText());
        try {
            us.setEstado((Estado) Model.getInstance().obtenerRegistro("obtenerEstadosPorId", interfaces.Constantes.ESTADO_ACTIVO));
            us.setTipo((TipoUsuario) Model.getInstance().obtenerRegistro("obtenerTipoUsuarioPorId",interfaces.Constantes.USUARIO_ADMINISTRADOR));
        } catch (Exception ex) {
            Logger.getLogger(FormularioUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return us;
    }

    public boolean validar() {

        if (txtusuario.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el USUARIO", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtusuario.requestFocus();
            return false;
        } else if (tpfpassword.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el PASSWORD por favor", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            tpfpassword.requestFocus();
            return false;
        } else if (tpfrpassword.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Repita el PASSWORD por favor", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            tpfrpassword.requestFocus();
            return false;
        } else if (!tpfrpassword.getText().equals(tpfpassword.getText())) {
            JOptionPane.showMessageDialog(null, "Las PASSWORD ingresadas no coinciden", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            tpfrpassword.requestFocus();
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField tpfpassword;
    private javax.swing.JPasswordField tpfrpassword;
    private javax.swing.JTextField txtusuario;
    // End of variables declaration//GEN-END:variables
}
